import java.io.*; 
import java.util.*;
import java.lang.*;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Random;
import java.util.Scanner;


public class MainSwinwise
 {
	public static String userName = null;
	public static String[] result=new String [500];
	public static String DayNow = null;
	public static String searchStr =null;


	public static void SubmitInputHandle()
	 {
    
      	System.out.print("Please Enter your Student Id, Subject Name ,Assignment Name,File Path seperated by commas : ");
	      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  

       try { 
        	 userName = br.readLine(); 
      	} catch (IOException ioe)
		 { 
         		System.out.println("Error while submitting your assignment, please try again!"); 
         		SubmitInputHandle();
      	}

                int idx=0; int tokenCount;
                result = userName.split(",");

	}

	public static void ViewDetailsInputHandle()
	{
	   try
		{
                String [] Parameters =null;
        		BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));       
    			while (System.in.available() > 0) //check enter
                	System.in.read(); //flush the buffer
                
                	System.out.println("Enter StudentId Subject Name & Assignment Name with spaces");
                	System.out.print(">");	
     			searchStr=inFromUser.readLine(); // wait for user input
     			
     			
     			if(!searchStr.equalsIgnoreCase("exit")) //if user doesn't type 'exit'
     			{
     				Parameters=searchStr.split("\\s"); //get each parameter and put in an array
     				if(Parameters.length!=3) //whether user entered 3 parameters?
     				{     				
     					System.out.println("insufficient parameters."); //if not, notify and exit
     					System.exit(1);
     				}
     			}  
				
   
		
		}catch(Exception ioe)
		 {
			System.out.println("Error while view the details, please try again!"); 
			ViewDetailsInputHandle();
		 }

	}

    

      public static void ViewMarksInputHandle() {
    
      		System.out.print("Please Enter your Student Id, Subject Name and Assignment Name seperated by commas : "); 

      
      		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  

      
      try { 
         	userName = br.readLine(); 
      	  } catch (IOException ioe) { 
         	System.out.println("Error while submitting your assignment, please try again!"); 
               ViewMarksInputHandle();
      	  }


 	      int idx=0; int tokenCount;
              result = userName.split(",");

	}

     public static void DownloadAssignmentsInputHandle()
     {
         System.out.print("Please type the assignment you wish to download : "); 
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

      
      try { 
         userName = br.readLine(); 
      } catch (IOException ioe) { 
         System.out.println("Error while downloading your assignment, please try again!"); 
         DownloadAssignmentsInputHandle();
      }
    	int idx=0; int tokenCount;
       result = userName.split(",");
       
     }

    public static void main(String[] args)
     {

      try
      { 
        Subject sbjObj = null;
        Student stdObj = null;
	    MarksFile marksFileObj = null;
        Marking markingObj = null;
        
        
        Assignment assignmentObj = null;
        Tasks tsk = new Tasks();
	    Random generator = new Random();
        int i = generator.nextInt(1000);
        String assignment_Id = Integer.toString(i);


	    if(args[0].equalsIgnoreCase("Submit"))
         {
        	//this will submit the assignment  
		
		    SubmitInputHandle();
		    stdObj = new Student(result[0],"watte","weliwita");
        	sbjObj = new Subject("AGILE1",result[1]);
		    assignmentObj = new Assignment(sbjObj, stdObj, result[2], assignment_Id);
         
            System.out.println(result[3]);
        	tsk.submitAssignment(assignmentObj);
            tsk.UploadSubmission(result[3],stdObj.getStudentId(),sbjObj.getName(),assignmentObj.getName());
        	System.out.println("Assignment Submitted & your receipt no is :" + assignment_Id);
        }
	    else if(args[0].equalsIgnoreCase("View"))
	    {
        	//this will view the details of a perticular assignment
	 	 ViewDetailsInputHandle();
	 
        	tsk.viewAssignmentDetails(searchStr); 

	   }else if(args[0].equalsIgnoreCase("ViewMarks"))
	   {
		
        	//this will view the details of a perticular assignment
	 	 ViewMarksInputHandle();
	 	     marksFileObj = new MarksFile(result[0],result[1],result[2]);
        	tsk.ViewMarKDetails(marksFileObj); 
       }else if (args[0].equalsIgnoreCase("Marking"))
       {
            markingObj = new Marking();
             HandleInput inUser= new HandleInput();
             inUser.HandleInput();

            markingObj.BaseMethod(inUser.gettheScheme(),inUser);
       }else if (args[0].equalsIgnoreCase("Download"))
       {
           System.out.println("following are the available assignments to download");
           System.out.println(" ");
            File file = new File("C:\\java proj\\Assignment Sheet\\assignment list.txt");
                FileInputStream fis = null;
                BufferedInputStream bis = null;
                DataInputStream dis = null;

                fis = new FileInputStream(file);
               // Here BufferedInputStream is added for fast reading.
                bis = new BufferedInputStream(fis);
                dis = new DataInputStream(bis);
                // dis.available() returns 0 if the file does not have more lines.
                while (dis.available() != 0) {

                 // this statement reads the line from the file and print it to
                 // the console.
                  System.out.println(dis.readLine());
            }

            DownloadAssignmentsInputHandle();

            tsk.DownloadFile(result[0]);
        }else
		{
			System.out.println("Invalid Option...!");
		}

        }catch (ArrayIndexOutOfBoundsException Ex)
         {
            System.out.println("No Valid Input...!");
	
         }catch (Exception e)
       {
         //System.out.println("Invalid Arguments");
       }        
    }
}